package com.caption.jdk.lambda;

import java.util.*;

public class TestLambda {
    public static void main(String[] args) {
        //使用匿名内部类方式运行
        executeShow(new UserService() {
            @Override
            public void show(String name) {
                System.out.println("匿名内部类方式执行。");
            }
        },"abc");

        //使用lambda表达式
        executeShow((name)->{
            System.out.println("lambda表达式执行");
        },"ssa");

        List<Person> list = new ArrayList<>() ;
        list.add(new Person("张三",30));
        list.add(new Person("李四",20));
        list.add(new Person("王五",25));
        //使用匿名内部类方式进行排序
       /* Collections.sort(list, new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.getAge()-o2.getAge();
            }
        });
        for(Person person:list){
            System.out.println(person);
        }
        */

        //lambda表达式只有一行时可以把大括号去掉
        Collections.sort(list,(Person o1,Person o2)-> o2.getAge()-o1.getAge());
        for(Person person:list){
            System.out.println(person);
        }
    }
    public static  void executeShow(UserService userService,String name){
        userService.show(name);
    }
}
